/***************************************************************************
 *   This file is part of KLabeler                                         *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "ktagicon.h"
#include <QUrl>
#include <QGraphicsSceneDragDropEvent>
#include <QPainter>

#ifdef  _DEBUG_ACTIONS
#define D(x)    x
#else
#define D(x)
#endif


KTagIcon::KTagIcon(QGraphicsItem * parent) : Plasma::IconWidget(parent), _newBrush(QColor(255, 0, 0))
{
    _isBrush = false;
    setAcceptDrops(true);
}

void KTagIcon::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
    D(printf("dragEnterEvent\n"));
    if (event->mimeData()->hasUrls()) {
        // check for local files
        event->acceptProposedAction();
        hilite();
    }
}

void KTagIcon::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
{
    Plasma::IconWidget::dragLeaveEvent(event);
    unhilite();
}

void KTagIcon::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
{
    D(printf("dragMoveEvent\n"));
    hilite();
    Plasma::IconWidget::dragMoveEvent(event);
    event->accept();
}

void KTagIcon::dropEvent(QGraphicsSceneDragDropEvent * event)
{
    D(printf("DROPPED\n"));
    QStringList urls;
    if (event->mimeData()->hasUrls()) {
        D(printf("has urls\n"));
        foreach(QUrl url, event->mimeData()->urls()) {
            QString path = url.path();
            urls.append(path);
            D(printf(" new path %s\n", path.toAscii().data()));
        }
        emit droppedFiles(urls);
    }
    unhilite();
}

void KTagIcon::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Plasma::IconWidget::paint(painter, option, widget);
    if (_isBrush) {
        painter->setPen(Qt::red);
        QRectF bound = boundingRect() ;
        painter->drawRoundedRect(bound, 10, 10);
    }
}

void KTagIcon::hilite()
{
    if (!_isBrush) {
        _isBrush = true ;
        update();
    }
}

void KTagIcon::unhilite()
{
    if (_isBrush) {
        _isBrush = false ;
        update();
    }
}

